/*
 * utilities - consists of utility methods
 *
 * Copyright (c) 2008 Varun Kumar Reddy Dodla
 * Licensed under GPL (GPL-LICENSE.txt)
 *
 * $Date: 2008-10-26 
 */

/**
 * this is tested in firefox 3 only
 */
function fireInput(element, input) {
	
	var index = 0;
	for(index = 0; index < input.length; index++) {
		var code = input.charCodeAt(index);
		
		//create the keyboard event
		var eve = document.createEvent("KeyboardEvent");

		//first key press
		eve.initKeyEvent ('keydown', true, true, null, 
		                        false, false, false, false, 
		                        code, code);
		element.dispatchEvent(eve);
		
		//then key press event
		eve.initKeyEvent ('keypress', true, true, null, 
		                        false, false, false, false, 
		                        code, code);
		element.dispatchEvent(eve);
		
		//then key up event
		eve.initKeyEvent ('keyup', true, true, null, 
		                        false, false, false, false, 
		                        code, code);
		element.dispatchEvent(eve);
	}
}

function fireClear(element) {
	
	var input = element.value;
	var index = 0;
	for(index = 0; index < input.length; index++) {
		var code = 8;
		
		//create the keyboard event
		var eve = document.createEvent("KeyboardEvent");

		//first key press
		eve.initKeyEvent ('keydown', true, true, null, 
		                        false, false, false, false, 
		                        code, code);
		element.dispatchEvent(eve);
		
		//then key press event
		eve.initKeyEvent ('keypress', true, true, null, 
		                        false, false, false, false, 
		                        code, code);
		element.dispatchEvent(eve);
		
		//then key up event
		eve.initKeyEvent ('keyup', true, true, null, 
		                        false, false, false, false, 
		                        code, code);
		element.dispatchEvent(eve);
	}
}

function fireFocus(element) {
	//create the keyboard event
	var eve = document.createEvent("UIEvent");

	//first key press
	eve.initUIEvent ('focus', true, true, null, 0);
	element.dispatchEvent(eve);
}

function sleep(naptime){
    naptime = naptime * 1000;
    var sleeping = true;
    var now = new Date();
    var alarm;
    var startingMSeconds = now.getTime();
    while(sleeping){
       alarm = new Date();
       alarmMSeconds = alarm.getTime();
       if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
    }      
    return;
 }


